/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jebert.javamessage.control;

import java.io.DataOutputStream;
import java.net.Socket;
import org.jebert.javamessage.model.Contact;
import org.jebert.javamessage.view.ChatWindow;

/**
 *
 * @author jebert
 */
public class MessageSender extends Thread {

    private String messageToSend;
    private ChatWindow chatWindow;

    public MessageSender(String message, ChatWindow chatwindow) {
        this.messageToSend = message;
        this.chatWindow = chatwindow;
    }

    public void run() {
        while (true) {
            try {
                Contact contact = chatWindow.getWindowContact();
                Socket s = new Socket(contact.getHost(), contact.getPort());
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                dos.writeUTF(messageToSend);
                dos.flush();
                dos.close();

                System.out.println("enviado");

            } catch (Exception e) {
            }
        }

    }
}
